package team.bluepen.supermarket.service.recommend.service;

import lombok.extern.slf4j.Slf4j;
import team.bluepen.supermarket.data.entity.Product;
import team.bluepen.supermarket.service.recommend.dto.ItemDTO;
import team.bluepen.supermarket.service.recommend.dto.RelateDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YCB 2023/6/19 15:01
 */
@Slf4j
public class FileDataSource {

    /**
     * 方法描述: 读取基础数据（数据格式为: 用户ID，商品ID，评分，邮戳），将数据封装为便于传输的Bean对象
     *
     * @author YCB 2023/6/19 15:01
     */
    public static List<RelateDTO> getData(List<Product> products) {
        return products.stream()
                .map(FileDataSource::convertsToRelated)
                .collect(ArrayList::new, List::add, List::addAll);
    }


    private static RelateDTO convertsToRelated(Product product) {
        return new RelateDTO(
                product.getUserId() % 100,
                product.getId() % 100,
                getRating(product)
        );
    }

    private static ItemDTO convertsTo(Product product) {
        return new ItemDTO(
                product.getId() % 100,
                product.getName(),
                product.getId()
        );
    }


    /**
     * 方法描述: 读取商品数据
     *
     * @Return {@link List<ItemDTO>}
     */
    public static List<ItemDTO> getItemData(List<Product> products) {
        return products.stream()
                .map(FileDataSource::convertsTo)
                .collect(ArrayList::new, List::add, List::addAll);
    }


    private static double getRating(Product product) {
        return product.getId() % 5 + 1;
    }
}

